/* 系统管理-公共方法 */
srvMap.add('queryAreaTree','','front/sh/sysManager!execute?uid=queryAreaTree');//用户新增
var $M = {
	// 单击某一行，选中/不选中checkbox
	checkTr : function(obj) {
		return;
		var _o = $(obj);
		var _c = _o.find(".num-checkbox");

		// 状态保持相反
		if (_c.prop("checked")) {
			_c.prop("checked", false);
		} else {
			_c.prop("checked", true);
		}
	},

	// 全选
	checkAll : function(obj) {
		var _o = $(obj);
		var _checkboxs = _o.parents(".data-table").find(".num-checkbox");

		// 状态保持一致
		if (_o.prop("checked")) {
			_checkboxs.prop("checked", true);
		} else {
			_checkboxs.prop("checked", false);
		}
	},

	// 成功提示
	successTip : function(tip) {
		if (null == tip) {
			tip = "操作成功";
		}
		var str = '<span class="iconfont icon-zhengque1"></span>  <span>$1</span>';
		str = str.replace(/\$1/g, tip);
		$(".form-tip").addClass("form-tip-success").html(str);
	},

	// 失败提示
	failureTip : function(tip) {
		if (null == tip) {
			tip = "操作失败";
		}
		var str = '<span class="iconfont icon-cuowu1"></span>  <span>$1</span>';
		str = str.replace(/\$1/g, tip);
		$(".form-tip").addClass("form-tip-failure").html(str);
	},

	// 重置表单
	resetForm : function(id) {
		var _o = $(id);
		_o.find("input:not(:disabled)").each(function() {
			$(this).val("");
		});
		_o.find("textarea:not(:disabled)").each(function() {
			$(this).val("");
		});
	},

	// 美化textarea
	beautiTextarea : function(){
		var _o = $("textarea");
		var s = _o.val();
		s = s.replace(/\n/g, "");
		s = s.replace(/[;；]/g, ";\n");
		_o.val(s);
	}
};

$(function() {
	// 全选事件绑定
	$(document).delegate(".all-checkbox", "click", function() {
		$M.checkAll(this);
	}).delegate(".data-table tr", "click", function() {
		$M.checkTr(this);
	}).delegate(".num-checkbox", "click", function(e) {
		e.stopPropagation();
	});

	// 美化textarea事件绑定
	$(document).delegate("textarea", "keyup", function(event) {
		if(event.keyCode == 8){
			return;
		}
		$M.beautiTextarea();
	});
});

$(function() {
	$(window).resize(function(event) {
		// 显示地市树
		showAreaTree();
	});
	
	$(document).click(function(event) {
		var obj=this.body;
		var cNmae=$(obj).attr('class');
		var tId=event.target.id;
		 if(cNmae=="scope" || tId.indexOf("areaTree")>=0 || tId=="areaId"){
			 return;
		 }
		 var _o = $("#areaTree");
		 if(_o){
			_o.hide();
		 }
	});
	
	
	$(document).delegate('#areaId', 'focusin', function(event) {
		var _o = $("#areaTree");
		if (_o.children().size() > 0) {
			// 显示地市树
			showAreaTree();
		} else{
			var str = '<div id="areaTree" class="area-tree-box ztree"></div>';
			$("body").append(str);

			// 初始化地市树
			initAreaTree();
		};
	});
	
	
});

/**
 * 初始化地市树
 * @return {[type]} [description]
 */
function initAreaTree () {
//	var url = CONTENT_PATH
//			+ "/com.asiainfo.zhly.CommonAction.queryAreaTree.invoke";
//	Base.ajax({
//		url : url,
	var par="";
	Util.ajax.postJsonSync(srvMap.get('queryAreaTree'),par,function(json,status){
			if (null == json.object || null == json.object.result) {
				return;
			}

			// 加载地市树
			var data = json.object.result;
			loadAreaTree(data);
	});
}

/**
 * 加载地市树
 * 
 * @return {[type]} [description]
 */
function loadAreaTree (zNodes) {
	var setting = {
		view : {
			showIcon: false,
			showTitle: false
		},
		check: {
			enable: true,
			chkboxType : { "Y" : "", "N" : "" }
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onCheck: zTreeOnCheck
		}
	};

	treeObj = $.fn.zTree.init($("#areaTree"), setting, zNodes);
	treeObj.expandAll(true);

	// 显示地市树
	showAreaTree();
}

/**
 * 显示地市树
 * @return {[type]} [description]
 */
function showAreaTree(){
	var _o = $("#areaId");
	if(_o.length < 1){
		return;
	}

	var op = _o.position(),
		ol = op.left,
		ot = op.top,
		oh = _o.outerHeight();

	var _ob = $("#areaTree");
	_ob.css({
		left: ol,
		top: ot + oh,
		display: "block"
	});
}

/**
 * 隐藏地市树
 * @return {[type]} [description]
 */
function hideAreaTree () {
	var _ob = $("#areaTree");
	_ob.css({
		left: '-9999',
		top: '-9999'
	});
}

/**
 * 地市树check事件
 * @param  {[type]} event    [description]
 * @param  {[type]} treeId   [description]
 * @param  {[type]} treeNode [description]
 * @return {[type]}          [description]
 */
function zTreeOnCheck(event, treeId, treeNode) {
	var _o = $("#areaId"),
		areaId = treeNode.id,
		areaName = treeNode.name;
	_o.val(areaName).attr("areaId", areaId);
	//取消其他已勾选的节点
	treeObj.checkAllNodes(false);
	treeObj.checkNode(treeNode, true, true);

	//隐藏地市树
	hideAreaTree();
	$("#areaId").siblings('.form-error-tip').hide();
};